#include <stdio.h>
#include <stdlib.h>
#include "../../src/thread.h"

my_sem_t mutex;

void* affichage (void* name){
  int i;

  /*section critique, on pause un verou avant*/
  my_sem_wait(&mutex); /* prologue */
  printf("VERROU mis\n");

  for(i=0; i<5; i++) {  
    printf("%s\n",(char*)name);
    thread_yield();/*pour etre sur d'avoir des problemes*/  
  }

  /*on enleve le verou*/
  printf("VERROU levé\n");
  my_sem_post(&mutex); /* epilogue */

  thread_exit(NULL);
} 


int main (void){ 
  thread_t filsA, filsB;  
  void*a = "a";
  void*b = "b";
  
  my_sem_init(&mutex, 1);
  
  if (thread_create(&filsA, affichage, a)) { 
    perror("thread_create"); 
    exit(EXIT_FAILURE); 
  } 
  if (thread_create(&filsB, affichage, b)) { 
    perror("thread_create"); 
    exit(EXIT_FAILURE); 
  } 
  
  if (thread_join(filsA, NULL)) 
    perror("thread_join"); 
  
  if (thread_join(filsB, NULL)) 
    perror("thread_join"); 
  killMainThread();
  return (EXIT_SUCCESS);
}
